/**
 * 
 */
package t4j.concurrency.deadlock;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author sz
 *
 * create time：2013-3-15
 */
public class FixedDiningPhilosophers {

	public static void main(String[] args) throws Exception{
		int ponder = 5;
		
		int size = 3;
		
		ExecutorService exec = Executors.newCachedThreadPool();
		
		Chopstick[] sticks = new Chopstick[size];
		
		for (int i = 0; i < size; i++)
			sticks[i] = new Chopstick();
		
		for(int i= 0; i < size ; i++)
			if(i < (size -1))
				exec.execute(new Philosopher(sticks[i], sticks[i+1], i, 0));//那起右边先，再拿左边
			else
				exec.execute(new Philosopher(sticks[0], sticks[i], i, 0));//最后一个哲学家拿起左边筷子，才拿右边，这样就可以把右边筷子空余出来，破坏循环等待条件
		
		TimeUnit.SECONDS.sleep(10);
		
		exec.shutdownNow();
		
		
	}
}
